package com.innovation.sys.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.innovation.sys.common.enums.Res;
import com.innovation.sys.common.vo.BaseException;
import com.innovation.sys.common.vo.ResponseObject;
import com.innovation.sys.common.vo.ResultMap;
import com.innovation.sys.common.vo.Ret;
import com.innovation.sys.pojo.SSxsArticle;
import com.innovation.sys.pojo.SSysLog;
import com.innovation.sys.service.ArticleService;
import com.innovation.sys.service.LogAdminService;

@Controller
@RequestMapping("/api/admin/business/article")
public class ArticleAPIController {

	private static Logger logger = LoggerFactory.getLogger(ArticleAPIController.class);

	@Resource
	private ArticleService articleService;

	@Resource
	private LogAdminService logAdminService;

	@Resource
	private RedisTemplate<String, String> redisTemplate;

	@ResponseBody
	@RequestMapping(value = "/addArticle", method = { RequestMethod.POST })
	public String addOne(@RequestBody SSxsArticle article) {
		logger.info("添加文章业务开始===>");
		try {
			// 获取存入redis的用户信息
			ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
			String user = opsForValue.get("user");
			// 保存日志信息
			SSysLog sSysLog = new SSysLog();
			sSysLog.setCreateTime(new Date());
			sSysLog.setLogDesc("保存文章信息操作");
			sSysLog.setLogUser(user);
			logAdminService.addLog(sSysLog);
			article.setPubPerson(user);
			return articleService.add(article) > 0 ? JSON.toJSONString(new Ret().setOk(Res.COM_SUCCESS))
					: JSON.toJSONString(new Ret().setFail(Res.COM_FAILED));
		} catch (Exception e) {
			logger.info("添加文章失败,文章信息为:{}" + article.toString());
			Map<String, Object> map = new HashMap<>();
			map.put("code", Res.ADD_ARTICLE_FAILED.getCode());
			map.put("message", Res.ADD_ARTICLE_FAILED.getMsg());
			throw new BaseException(map);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/articleList", method = { RequestMethod.GET })
	public ResultMap<List<Map<String, Object>>> listOfArticle(HttpServletRequest request,
			@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
		logger.info("查询文章列表业务开始===>");
		Map map = new HashMap();
		map.put("title", request.getParameter("title"));
		// pageNum当前页，pageSize代表每页多少条记录
		PageHelper.startPage(pageNum, pageSize);
		// 使用PageInfo包装查询结果，返回给页面
		PageInfo<Map<String, Object>> selectByOption = articleService.selectByOption(map, pageNum, pageSize);
		List<Map<String, Object>> list = selectByOption.getList();
		return new ResultMap<List<Map<String, Object>>>(0, "", list, selectByOption.getTotal());
	}

	@RequestMapping(value = "/routeToArticleList", method = { RequestMethod.GET })
	public String routeToArticleList() {
		return "layuiAdmin/admin/article/article";
	}

	@ResponseBody
	@RequestMapping(value = "/removeArticle", method = { RequestMethod.POST })
	public String removeOne(@RequestBody Integer articleId) {
		logger.info("删除文章业务开始===>");
		try {
			// 获取存入redis的用户信息
			ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
			String user = opsForValue.get("user");
			// 保存日志信息
			SSysLog sSysLog = new SSysLog();
			sSysLog.setCreateTime(new Date());
			sSysLog.setLogDesc("删除文章信息操作");
			sSysLog.setLogUser(user);
			logAdminService.addLog(sSysLog);
			return articleService.deleteOne(articleId) > 0 ? JSON.toJSONString(new Ret().setOk(Res.COM_SUCCESS))
					: JSON.toJSONString(new Ret().setFail(Res.COM_FAILED));
		} catch (Exception e) {
			logger.info("删除id为:{}" + articleId + "文章失败！");
			Map<String, Object> map = new HashMap<>();
			map.put("code", Res.DEL_ARTICLE_FAILED.getCode());
			map.put("message", Res.DEL_ARTICLE_FAILED.getMsg());
			throw new BaseException(map);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/updateArtilce", method = { RequestMethod.POST })
	public String updateOne(@RequestBody SSxsArticle article) {
		logger.info("更改文章信息业务开始===>");
		try {
			// 获取存入redis的用户信息
			ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
			String user = opsForValue.get("user");
			// 保存日志信息
			SSysLog sSysLog = new SSysLog();
			sSysLog.setCreateTime(new Date());
			sSysLog.setLogDesc("更新文章信息操作");
			sSysLog.setLogUser(user);
			logAdminService.addLog(sSysLog);
			return articleService.updateOne(article) > 0 ? JSON.toJSONString(new Ret().setOk(Res.COM_SUCCESS))
					: JSON.toJSONString(new Ret().setFail(Res.COM_FAILED));
		} catch (Exception e) {
			logger.info("更改文章id为:{}" + article.getArticleId() + "失败!");
			Map<String, Object> map = new HashMap<>();
			map.put("code", Res.MOD_ARTICLE_FAILED.getCode());
			map.put("message", Res.MOD_ARTICLE_FAILED.getMsg());
			throw new BaseException(map);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/findOneArticle", method = { RequestMethod.POST })
	public ResponseObject findOneArticle(@RequestBody Integer articleId) {
		logger.info("查询文章业务开始===>");
		try {
			SSxsArticle findOne = articleService.findOne(articleId);
			if (findOne != null) {
				return new ResponseObject(Res.COM_SUCCESS.getCode(), Res.COM_SUCCESS.getMsg(), findOne);
			} else {
				return new ResponseObject(Res.COM_FAILED.getCode(), Res.COM_FAILED.getMsg());
			}
		} catch (Exception e) {
			logger.info("查询文章id为:{}" + articleId + "信息失败");
			Map<String, Object> map = new HashMap<>();
			map.put("code", Res.QRY_ARTICLE_FAILED.getCode());
			map.put("message", Res.QRY_ARTICLE_FAILED.getMsg());
			throw new BaseException(map);
		}
	}

}
